package com.datawale.vh.demo.work.controller;

import com.alibaba.fastjson.JSONObject;
import com.datawale.vh.demo.work.util.CommonUtil;
import com.datawale.vh.demo.work.util.HttpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author huangtao
 * @time 2023/6/17
 * @description
 */
@Controller
@RequestMapping("/backend")
public class BackendController {


    @RequestMapping(value = "/asr",method = RequestMethod.POST)
    @ResponseBody
    public String TestASR(JSONObject config) throws IOException {
        String filepath = "/root/asr.wav";
        JSONObject param = new JSONObject();
        String s = CommonUtil.fileToBase64(filepath);
        if (config.isEmpty()){
            param.put("audio",s);
            param.put("audio_format","wav");
            param.put("sample_rate",16000);
            param.put("lang","zh_cn");
            param.put("punc",0);
        }else {
            //todo
            param.put("audio",s);
            param.put("audio_format","wav");
            param.put("sample_rate",16000);
            param.put("lang","zh_cn");
            param.put("punc",0);
        }

        String post = HttpUtils.post("http://39.100.75.105:8090/paddlespeech/asr", param.toString());
        return post;
    }

    @RequestMapping(value = "/tts",method = RequestMethod.POST)
    @ResponseBody
    public String TestTTS(String text,JSONObject config) throws IOException {
        JSONObject param = new JSONObject();
        param.put("text",text);
        if (config.isEmpty()){
            param.put("spk_id",0);
            param.put("speed",1f);
            param.put("volume",1f);
            param.put("sample_rate",0);
            param.put("save_path","./tts1.wav");
        }else {
            //todo
        }

        String post = HttpUtils.post("http://39.100.75.105:8090/paddlespeech/tts", param.toString());
        return post;

    }
}
